import { SuggestionResponse, SuggestionResultResponse } from '@/types/data'
import { RootThunkAction } from '@/types/store'

import http from '@/utils/http'

export const getSuggestion = (q: string): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.get<SuggestionResponse>('/suggestion', {
      params: {
        q,
      },
    })
    console.log(res)
    dispatch({ type: 'search/suggestion', payload: res.data.data.options })
  }
}

export const clearSuggestion = () => ({ type: 'search/clearSuggestion' })

export const getSuggestionResult = (query: string): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.get<SuggestionResultResponse>('/search', {
      params: {
        page: 1,
        per_page: 10,
        q: query,
      },
    })
    console.log(res)
    dispatch({ type: 'search/getSuggestionResult', payload: res.data.data })
  }
}
